package com.hust.edu.cn.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 登录过滤拦截
 * 继承HandlerInterceptorAdapter适配器，重写预处理方法preHandle
 * @Author Yu Jian
 * @since 2021-07-06
 * */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        // 判断session里面是否有用户，没有的话重定向到登录页面，给拦截掉
        if(request.getSession().getAttribute("user") == null){
            response.sendRedirect("/admin");
            return false;
        }
        return true;
    }

}
